//
//  GoodsMessageLastViewController.m
//  MyProject
//
//  Created by dllo on 15/10/30.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "GoodsMessageLastViewController.h"
#import "GoodsWithMessageTableViewCell.h"
#import "UIImageView+WebCache.h"
#import <QuartzCore/QuartzCore.h>
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDKUI.h>
#import "GoodsWebViewController.h"
#import "GoodMessageTableViewCell.h"
#import "DataBase.h"
#import "Collect.h"
#import "ButifulArticle+CoreDataProperties.h"
#import "HandleData.h"
#import "GoodsMessage.h"


@interface GoodsMessageLastViewController ()<UITableViewDataSource,UITableViewDelegate ,UIScrollViewDelegate>

@property (nonatomic,strong) UITableView *tableView;
@property (nonatomic ,strong) UIView *animaView;
@property (nonatomic ,strong) UIButton *clickButton;
@property (nonatomic ,strong) UIVisualEffectView *visualView;
@property (nonatomic,strong) UIScrollView *scrollView;
@property (nonatomic ,assign) NSInteger rect;
@property (nonatomic,strong) NSMutableArray *array;
@end

@implementation GoodsMessageLastViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
  //  [self.navigationController.navigationBar setTitleTextAttributes:[NSDictionary dictionaryWithObjectsAndKeys:[UIColor whiteColor],NSForegroundColorAttributeName,nil]];
    
    [self createTableView];
    [self handleData];
    self.visualView = [[UIVisualEffectView alloc]initWithEffect:[UIBlurEffect effectWithStyle:UIBlurEffectStyleLight]];
    self.visualView.frame = self.view.bounds;
    [self.view addSubview:_visualView];
    self.visualView.layer.cornerRadius = 8;
    self.visualView.clipsToBounds = YES;
    self.visualView.hidden = YES;
    
    UIButton *button1 = [UIButton buttonWithType:UIButtonTypeSystem];
    button1.frame = CGRectMake(self.visualView.bounds.size.width - 50, -40, 40, 40);
    
    [button1 setTitle:@"分享" forState:UIControlStateNormal];
    [button1 addTarget:self action:@selector(share:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button1];
    button1.tag = 100;
    button1.layer.cornerRadius = 320/16;
    [button1 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    button1.backgroundColor = [UIColor redColor];
    
    
    
    UIButton *button2 = [UIButton buttonWithType:UIButtonTypeSystem];
    button2.frame = CGRectMake(self.visualView.bounds.size.width - 50, -40, 40, 40);
    
    [button2 setTitle:@"收藏" forState:UIControlStateNormal];
    [button2 addTarget:self action:@selector(collection:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button2];
    button2.tag = 101;
    button2.layer.cornerRadius = 320/16;
    button2.backgroundColor = [UIColor redColor];
    [button2 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    

    
    

    UIButton *button3 = [UIButton buttonWithType:UIButtonTypeSystem];
    button3.frame = CGRectMake(self.visualView.bounds.size.width - 50,-40, 40, 40);
    
    [button3 setTitle:@"取消" forState:UIControlStateNormal];
    [button3 addTarget:self action:@selector(cancle:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button3];
    button3.tag = 102;
    button3.layer.cornerRadius = 320/16;
    button3.backgroundColor = [UIColor redColor];
    [button3 setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"me.png"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)];
}

//夜间
- (void)viewWillAppear:(BOOL)animated
{
    if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
        self.tableView.backgroundColor = [UIColor grayColor];
    }
    else {
        self.tableView.backgroundColor = [UIColor whiteColor];
    }
    self.tabBarController.tabBar.hidden = NO;
    [self saveColor];
    [self.tableView reloadData];
    
}


// 收藏
- (void)collection:(UIButton *)but
{
  DataBase *coreData = [DataBase dataBase];
    
    NSFetchRequest *request = [NSFetchRequest fetchRequestWithEntityName:@"Collect"];
    NSLog(@"%@",NSHomeDirectory());

    NSArray *arr = [coreData.managedObjectContext executeFetchRequest:request error:nil];
    NSMutableArray *array = [NSMutableArray array];
    for (Collect *collection in arr) {
    [array addObject:collection.photo];
    }
        if (![array containsObject:self.good.url]) {
        Collect *collect = [NSEntityDescription insertNewObjectForEntityForName:@"Collect" inManagedObjectContext:coreData.managedObjectContext];
            collect.title = self.good.content;
            collect.photo = self.good.url;
            collect.nameId = self.good.nameId;
            [coreData saveContext];
            NSLog(@"===========%@",collect.nameId);

            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"你已经收藏成功" preferredStyle:UIAlertControllerStyleAlert];
            
            [self presentViewController:alertView animated:YES completion:^{
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"好" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertView addAction:action];
                       }
        else{
            UIAlertController *alertView = [UIAlertController alertControllerWithTitle:@"提示" message:@"你已经收藏过了" preferredStyle:UIAlertControllerStyleAlert];
            [self presentViewController:alertView animated:YES completion:^{
                
            }];
            UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            }];
            [alertView addAction:action];
        }
}


// 取消
- (void)cancle:(UIButton *)but
{
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    UIButton *button1 = (UIButton *)[self.view viewWithTag:101];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:102];
    if (button) {
        
        [UIView animateWithDuration:0.6 animations:^{
            button.frame = CGRectMake(self.visualView.bounds.size.width - 50, -40, 40, 40);
        }];
    }
    if (button1) {
        
        [UIView animateWithDuration:0.5 animations:^{
            button1.frame = CGRectMake(self.visualView.bounds.size.width - 50, -40, 40, 40);;
        }];
    }
    if (button2) {
        
        [UIView animateWithDuration:0.4 animations:^{
            button2.frame = CGRectMake(self.visualView.bounds.size.width - 50,-40, 40, 40);;
        }];
    }
 }

//菜单实现:
- (void)click:(UIBarButtonItem *)click
{
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    UIButton *button1 = (UIButton *)[self.view viewWithTag:101];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:102];
    if (button) {
        
        [UIView animateWithDuration:0.4 animations:^{
            button.frame = CGRectMake(self.visualView.bounds.size.width - 50, 70, 40, 40);
        }];
    }
    if (button1) {
        
        [UIView animateWithDuration:0.5 animations:^{
            button1.frame = CGRectMake(self.visualView.bounds.size.width - 50, 120, 40, 40);;
        }];
    }
    if (button2) {
        
        [UIView animateWithDuration:0.6 animations:^{
            button2.frame = CGRectMake(self.visualView.bounds.size.width - 50,170, 40, 40);;
        }];
    }

    
    
}



//分享:

- (void)share:(UIButton *)but
{
    
    // 1.创建分享参数
    NSMutableDictionary *shareParames = [NSMutableDictionary dictionary];
    
    if (self.good != nil) {
        
        [shareParames SSDKSetupShareParamsByText:self.good.content images:@[self.good.url] url:nil title:@"分享" type:SSDKContentTypeImage];
    }
    
    //进行分享
    [ShareSDK showShareActionSheet:but items:nil
                       shareParams:shareParames
               onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
                   switch (state) {
                       case SSDKResponseStateBegin:
                       {
                           NSLog(@"开始分享");
                       }
                           break;
                       case SSDKResponseStateSuccess:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享成功"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       case SSDKResponseStateFail:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享失败"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       case SSDKResponseStateCancel:
                       {
                           UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"分享取消"
                                                                               message:nil
                                                                              delegate:nil
                                                                     cancelButtonTitle:@"确定"
                                                                     otherButtonTitles:nil];
                           [alertView show];
                       }
                           break;
                       default:
                           break;
                   }
               }];
}






- (void)createTableView
{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.tableView registerClass:[GoodsWithMessageTableViewCell class] forCellReuseIdentifier:@"name"];
    [self.tableView registerClass:[GoodMessageTableViewCell class] forCellReuseIdentifier:@"name1"];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.delegate  = self;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
         GoodsWithMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name"];
        [cell.userImageView sd_setImageWithURL:[NSURL URLWithString:self.good.avatar1] placeholderImage:[UIImage imageNamed:@"lovebeijing.png"]];
        cell.nameLable.text = self.good.username;
        cell.timeLable.text = self.good.datestr;
        [cell.bigImageView sd_setImageWithURL:[NSURL URLWithString:self.good.url] placeholderImage:[UIImage imageNamed:@"lovebeijing.png"]];
        //cell.messageLable.numberOfLines = 0;
        //cell.messageLable.text = self.good.content;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
            cell.nameLable.textColor = [UIColor whiteColor];
            cell.timeLable.textColor = [UIColor whiteColor];
        }
        else
        {
            cell.nameLable.textColor = [UIColor blackColor];
            cell.timeLable.textColor = [UIColor blackColor];
        }
        return cell;
    }
    else{
        GoodMessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"name1"];
        cell.messageLable.text = self.good.content;
        
        cell.backgroundColor = [UIColor clearColor];
        
        if (![[NSUserDefaults standardUserDefaults]boolForKey:@"isWhite"]) {
            cell.messageLable.textColor = [UIColor whiteColor];
        }
        else
        {
            cell.messageLable.textColor = [UIColor blackColor];
        }
        return cell;
   }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return self.view.bounds.size.width + 51;
    }
    else{
    CGRect rect = [self.good.content boundingRectWithSize:CGSizeMake([UIScreen mainScreen].bounds.size.width - 20, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14]} context:nil];
    self.rect = rect.size.height;
    return rect.size.height + 11;
    }
}


- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
   
}


- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    UIButton *button = (UIButton *)[self.view viewWithTag:100];
    UIButton *button1 = (UIButton *)[self.view viewWithTag:101];
    UIButton *button2 = (UIButton *)[self.view viewWithTag:102];
    if (button) {
        
        [UIView animateWithDuration:0.4 animations:^{
            button.frame = CGRectMake(self.visualView.bounds.size.width - 50, 70, 40, 40);
        }];
    }
    if (button1) {
        
        [UIView animateWithDuration:0.5 animations:^{
            button1.frame = CGRectMake(self.visualView.bounds.size.width - 50, 120, 40, 40);;
        }];
    }
    if (button2) {
        
        [UIView animateWithDuration:0.6 animations:^{
            button2.frame = CGRectMake(self.visualView.bounds.size.width - 50,170, 40, 40);;
        }];
    }
}

- (void)handleData
{
    NSString *str = [NSString stringWithFormat:@"http://open4.bantangapp.com/comm/comments/list?client_id=bt_app_android&client_secret=ffcda7a1c4ff338e05c42e7972ba7b8d&track_user_id=1115876&oauth_token=08e623418ebf12fe438fb9eed5d176da&track_deviceid=864516020760583&track_device_info=SM701&channel_name=UMENG_CHANNEL&app_installtime=1438818737013&app_versions=4.1.3&os_versions=4.4.2&screensize=1080&v=6&type_id=2&object_id=%@&page=0&pagesize=10",self.nameId];
    [HandleData handleDataWithUrl:str completion:^(id data) {
     NSDictionary *dic = [data objectForKey:@"data"];
        self.array = [NSMutableArray array];

        GoodsMessage *good = [[GoodsMessage alloc]init];
        [good setValuesForKeysWithDictionary:dic];
        [self.array addObject:good];
        [self.tableView reloadData];
        
           }];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
